package com.wqc.common.util;

import com.wqc.common.constant.CommonConstants;
import com.wqc.model.Dictionary;
import com.wqc.service.DictionaryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;

public class DictUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(DictUtils.class);

    public static boolean hasTypeAndKey(String key) {
        RedisTemplate redisTemplate = (RedisTemplate) SpringContextUtil.getBean("redisTemplate");
        return redisTemplate.hasKey(CommonConstants.DICT_TYPEANDKEY+key);
    }

    /**
     * hash类型 get操作 typeAndKey
     * @param typeAndKey
     * @param hashKey
     * @return
     */
    public static Object hashGetTypeAndKey(String typeAndKey, String hashKey) {
        RedisTemplate redisTemplate = (RedisTemplate) SpringContextUtil.getBean("redisTemplate");
        return redisTemplate.opsForHash().get(CommonConstants.DICT_TYPEANDKEY + typeAndKey, hashKey);
    }

    /**
     * hash类型 put操作 typeAndKey
     * @param typeAndKey
     * @param hashKey
     * @param hashValue
     * @return
     */
    public static void hashPutTypeAndKey(String typeAndKey, String hashKey, Object hashValue) {
        RedisTemplate redisTemplate = (RedisTemplate) SpringContextUtil.getBean("redisTemplate");
        redisTemplate.opsForHash().put(CommonConstants.DICT_TYPEANDKEY + typeAndKey, hashKey, hashValue);
    }

    /**
     * set类型 add操作 type
     * @param type
     * @param object
     * @return
     */
    public static void setAddType(String type, Object object) {
        RedisTemplate redisTemplate = (RedisTemplate) SpringContextUtil.getBean("redisTemplate");
        redisTemplate.opsForSet().add(CommonConstants.DICT_TYPE+type+":"+CommonConstants.LANG_TYPE_ZHCN, object);//get(CommonConstants.DICT_TYPE+key+"zh-CN");
    }

    /**
     * set类型 add操作 type
     * @param type
     * @return
     */
    public static Object setPopType(String type) {
        RedisTemplate redisTemplate = (RedisTemplate) SpringContextUtil.getBean("redisTemplate");
        return redisTemplate.opsForSet().pop(CommonConstants.DICT_TYPE+type+":"+CommonConstants.LANG_TYPE_ZHCN);//get(CommonConstants.DICT_TYPE+key+"zh-CN");
    }


    public static String getDictValue(String key) {
        Dictionary dictionary = getDictionary(key);
        return dictionary == null ? null:dictionary.getDicvalue();
    }

    public static String getDictDesc(String key) {
        Dictionary dictionary = getDictionary(key);
        return dictionary == null ? null:dictionary.getDescription();
    }

    private static Dictionary getDictionary(String key) {
        boolean hasKey = hasTypeAndKey(key);
        if (hasKey) {
            LOGGER.info("查询缓存...");
            Dictionary dict = (Dictionary)hashGetTypeAndKey(key, CommonConstants.LANG_TYPE_ZHCN);
            return dict;
        }
        else {
            LOGGER.info("查询数据库...");
            // 查询数据库
            DictionaryService dictionaryService = (DictionaryService)SpringContextUtil.getBean("dictionaryServiceImpl");
            Dictionary dictionary = dictionaryService.getDictionaryByTypeAndKey(key);
            String langType = CommonConstants.LANG_TYPE_ZHCN;
            if (dictionary != null) {
                langType = dictionary.getLangtype();
            }
            // 存入缓存
            hashPutTypeAndKey(key, langType, dictionary);
            return dictionary;
        }
    }
}
